﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Markup;
using System.Globalization;
using System.Data;
using ChoWorkflowServer.Model;
using Cinchoo.Core.ETL;

namespace ChoWorkflowDashboard
{
    public class TasksActionMenuEnabledMultiConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            if (values == null || values.Length != 2) return false;
            if (!(values[0] is bool) || !(values[0] is bool)) return false;

            return (bool)values[0] && (bool)values[1];
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            return new object[] { value };
        }
    }

    public class TasksActionMenuEnabledByAdminConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null) return false;

            string actionCmd = parameter as string;
            if (actionCmd.IsNullOrWhiteSpace()) return false;

            if (actionCmd == "Start")
            {
                return ChoMainWindow.Instance.IsStartTaskEnabled;
            }
            if (actionCmd == "Resume")
            {
                return ChoMainWindow.Instance.IsResumeTaskEnabled;
            }
            if (actionCmd == "Inactivate")
            {
                return ChoMainWindow.Instance.IsInactivateTaskEnabled;
            }
            else if (actionCmd == "Stop")
            {
                return ChoMainWindow.Instance.IsStopTaskEnabled;
            }
            else if (actionCmd == "Abort")
            {
                return ChoMainWindow.Instance.IsAbortTaskEnabled;
            }
            else if (actionCmd == "Activate")
            {
                return ChoMainWindow.Instance.IsActivateTaskEnabled;
            }

            return false;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return value;
        }
    }

    public class TasksActionMenuEnabledConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null) return false;

            //DataRowView row = value as DataRowView;
            ChoTasksDataSet.CHO_TASKSRow row = ((DataRowView)value).Row as ChoTasksDataSet.CHO_TASKSRow;

            string actionCmd = parameter as string;
            if (actionCmd.IsNullOrWhiteSpace() || row == null) return false;
            ChoETLWorkflowStatus taskStatus = (ChoETLWorkflowStatus)row.TASK_STATUS;

            if (actionCmd == "Start")
            {
                if (taskStatus == ChoETLWorkflowStatus.ACTIVE
                    //|| taskStatus == ChoETLWorkflowStatus.STOPPED
                    //|| taskStatus == ChoETLWorkflowStatus.SUCCESS
                    //|| taskStatus == ChoETLWorkflowStatus.FAILED
                    //|| taskStatus == ChoETLWorkflowStatus.TIMEDOUT
                    //|| taskStatus == ChoETLWorkflowStatus.ABORTED
                    )
                    return true;
            }
            else if (actionCmd == "Resume")
            {
                if (taskStatus == ChoETLWorkflowStatus.STOPPED
                    || taskStatus == ChoETLWorkflowStatus.FAILED
                    || taskStatus == ChoETLWorkflowStatus.TIMEDOUT
                    || taskStatus == ChoETLWorkflowStatus.ABORTED
                    )
                    return true;
            }
            else if (actionCmd == "Stop")
            {
                if (taskStatus == ChoETLWorkflowStatus.RUNNING)
                    return true;
            }
            else if (actionCmd == "Abort")
            {
                //if (taskStatus == ChoETLWorkflowStatus.RUNNING)
                    return true;
            }
            else if (actionCmd == "Activate")
            {
                if (taskStatus == ChoETLWorkflowStatus.INACTIVE
                    || taskStatus == ChoETLWorkflowStatus.STOPPED
                    || taskStatus == ChoETLWorkflowStatus.SUCCESS
                    || taskStatus == ChoETLWorkflowStatus.FAILED
                    || taskStatus == ChoETLWorkflowStatus.TIMEDOUT
                    || taskStatus == ChoETLWorkflowStatus.ABORTED
                    )
                    return true;
            }
            else if (actionCmd == "Inactivate")
            {
                if (taskStatus == ChoETLWorkflowStatus.ACTIVE
                    || taskStatus == ChoETLWorkflowStatus.STOPPED
                    || taskStatus == ChoETLWorkflowStatus.SUCCESS
                    || taskStatus == ChoETLWorkflowStatus.FAILED
                    || taskStatus == ChoETLWorkflowStatus.TIMEDOUT
                    || taskStatus == ChoETLWorkflowStatus.ABORTED
                    )
                    return true;
            }

            return false;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return value;
        }
    }

    partial class DataGridResource : ResourceDictionary
    { 
       public DataGridResource()
       {
          InitializeComponent();
       }

       void OnStartTask(object sender, RoutedEventArgs e)
       {
           ChoTasksDataSet.CHO_TASKSRow row = ChoMainWindow.Instance.SelectedRow.Row as ChoTasksDataSet.CHO_TASKSRow;
           if (row == null) return;

           ChoMultilineInputWindow wnd = new ChoMultilineInputWindow(row.IsCMD_PARAMETERSNull() ? null : row.CMD_PARAMETERS, "Enter command parameters");
           ChoMainWindow.ChildWindow = wnd;
           bool? ret = wnd.ShowDialog();
           if (ret == null || !ret.Value)
               return;

           string cmdParameters = wnd.InputText;

           ChoMainWindow.SendTaskActionCommand(ChoTaskActionType.StartTask, cmdParameters);
       }

       void OnResumeTask(object sender, RoutedEventArgs e)
       {
           ChoMainWindow.SendTaskActionCommand(ChoTaskActionType.ResumeTask);
       }

       void OnStopTask(object sender, RoutedEventArgs e)
       {
           ChoMainWindow.SendTaskActionCommand(ChoTaskActionType.StopTask);
       }

       void OnAbortTask(object sender, RoutedEventArgs e)
       {
           ChoMainWindow.SendTaskActionCommand(ChoTaskActionType.AbortTask);
       }

       void OnActivateTask(object sender, RoutedEventArgs e)
       {
           ChoMainWindow.SendTaskActionCommand(ChoTaskActionType.ActivateTask);
       }

       void OnInactivateTask(object sender, RoutedEventArgs e)
       {
           ChoMainWindow.SendTaskActionCommand(ChoTaskActionType.InactivateTask);
       }
    }
}
